package application.gui.adminWindow;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.table.TableModel;
import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.Admin;
import application.LoginAndAdministrationModule.PersonData;
import application.client.data.Client;
import application.parcelModule.Parcel;

public class AdminPanel extends JPanel{
	public AdminPanel(AdminHomePage parent, Communication db,
			Admin a, int type){
		this.parent = parent;
		this.db = db;
		this.admin = a;
		this.panelType = type;
		init();
	}
	
	private void init() {
		setSize(600, 480);
		setLayout(null);
		
		
		model = new TableModel(data, workerCollumnNames);
		clientTable = new JTable(model);
		clientTable.getTableHeader().setReorderingAllowed(false);
		pane = new JScrollPane(clientTable);
		

		addComponent(clientTable.getTableHeader(), 30, 70, 500, 30);
		addComponent(pane, 30, 100, 500, 300);
		if (panelType==PARCEL_PANEL){
			parcelCollumnNames = new String[]{ "ID", "Data nadania", "Imię i nazwisko Odbiorcy",
						"Cena", "Status" };
			filterInfo = new JLabel("Filtr");
			filter = new JComboBox<String>(new String[]{"Wszystkie","Nieaktywne","W dostarczeniu", "Dostarczone"});
			
			addComponent(filterInfo, 30, 30, 80, 30);
			addComponent(filter, 130, 30, 120, 30);
			
			initData(0);
			filter.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					initData(filter.getSelectedIndex());
				}
			});
			
		}
			else{
				workerCollumnNames = new String[]{ "Id","Imię i nazwisko","Email",
							"Telefon", "Adres", "Kod pocztowy", "Miasto" };
				initData(4);
			}
		setListeners();
	}
	
	private void addComponent(JComponent comp, int x, int y, int width,
			int height) {
		add(comp);
		comp.setBounds(x, y, width, height);
	}

	private void initData(int choice) {
		
		LinkedList<String[]> list = new LinkedList<String[]>();
		data = null;
		
		if(choice==0){
			ArrayList<Parcel> container = new ArrayList<Parcel>();	
			try {
				container = db.getAllParcel();
			
			for(Parcel p: container){
				Client c = db.getClientsByField("id",
						String.valueOf(p.getRecipient())).get(0);
				String[] row = { String.valueOf(p.getParcelID()),
						p.getDate(),
						c.getFirstName() + " " + c.getLastName(),
						String.valueOf(p.getPrice()),
						db.getParcelStatus(p.getParcelID()) };
				list.add(row);
			}
			data = list.toArray(new String[5][list.size()]);
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		else if(choice==1){
			ArrayList<Parcel> container = new ArrayList<Parcel>();	
			try {
				container = db.getParcelsByStatus(1);
			for(Parcel p: container){
				Client c = db.getClientsByField("id",
						String.valueOf(p.getRecipient())).get(0);
				String[] row = { String.valueOf(p.getParcelID()),
						p.getDate(),
						c.getFirstName() + " " + c.getLastName(),
						String.valueOf(p.getPrice()),
						db.getParcelStatus(p.getParcelID()) };
				list.add(row);
			}
			data = list.toArray(new String[5][list.size()]);
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		else if(choice==2){
			ArrayList<Parcel> container = new ArrayList<Parcel>();	
			try {
				container = db.getParcelsByStatus(2);
			
			for(Parcel p: container){
				Client c = db.getClientsByField("id",
						String.valueOf(p.getRecipient())).get(0);
				String[] row = { String.valueOf(p.getParcelID()),
						p.getDate(),
						c.getFirstName() + " " + c.getLastName(),
						String.valueOf(p.getPrice()),
						db.getParcelStatus(p.getParcelID()) };
				list.add(row);
			}
			container = db.getParcelsByStatus(3);
			for(Parcel p: container){
				Client c = db.getClientsByField("id",
						String.valueOf(p.getRecipient())).get(0);
				String[] row = { String.valueOf(p.getParcelID()),
						p.getDate(),
						c.getFirstName() + " " + c.getLastName(),
						String.valueOf(p.getPrice()),
						db.getParcelStatus(p.getParcelID()) };
				list.add(row);
			}
			data = list.toArray(new String[5][list.size()]);
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		else if(choice==3){
			ArrayList<Parcel> container = new ArrayList<Parcel>();	
			try {
				container = db.getParcelsByStatus(4);
			
			for(Parcel p: container){
				Client c = db.getClientsByField("id",
						String.valueOf(p.getRecipient())).get(0);
				String[] row = { String.valueOf(p.getParcelID()),
						p.getDate(),
						c.getFirstName() + " " + c.getLastName(),
						String.valueOf(p.getPrice()),
						db.getParcelStatus(p.getParcelID()) };
				list.add(row);
				data = list.toArray(new String[5][list.size()]);
			}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			
		}
		
		else if (choice == 4){
			try {
				ArrayList<PersonData> container = new ArrayList<PersonData>();
				container = admin.show_users(2);
				for (PersonData c : container) {
					//Imię i nazwisko", "Firma", "Email","Telefon", "Adres", "Kod pocztowy", "Miasto"
					list.add(new String[]{String.valueOf(c.getId()),c.getName()+" "+c.getSurname(),c.geteMail(),c.getephoneNumber(),c.getAddress().getStreet()+" "+c.getAddress().getFlatNumber()+"/"+c.getAddress().getHouseNumber(),c.getAddress().getPostCode(),c.getAddress().getCity()});
				}
				data = list.toArray(new String[7][list.size()]);
			} catch (SQLException e) {
				
				e.printStackTrace();
			}

		}
		remove(pane);
		remove(clientTable.getTableHeader());
		repaint();
		validate();
		if (panelType==PARCEL_PANEL){
		model = new TableModel(data, parcelCollumnNames);
		}else{
			model = new TableModel(data, workerCollumnNames);
		}
		clientTable = new JTable(model);
		clientTable.getTableHeader().setReorderingAllowed(false);
		pane = new JScrollPane(clientTable);
		addComponent(clientTable.getTableHeader(), 30, 70, 500, 30);
		addComponent(pane, 30, 100, 500, 300);
		if (list.size() == 0) {
			remove(pane);
		}
		repaint();
		validate();
	}
	
	private void setListeners(){
		
		
		clientTable.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				if (panelType==1){
				if (e.getClickCount() == 2) {
					EventQueue.invokeLater(new Runnable() {
						public void run() {
							ParcelDetails page = null;
							try {
								page = new ParcelDetails(
										parent,
										db,
										admin.getPersonData(),
										db.getParcelById(Integer.parseInt(String.valueOf(clientTable
												.getValueAt(clientTable
														.getSelectedRow(), 0)))));
							} catch (NumberFormatException e) {
								
								e.printStackTrace();
							} catch (SQLException e) {
								
								e.printStackTrace();
							}
							page.setVisible(true);
							parent.setVisible(false);
						}
					});
				}
			}else{
				if (e.getClickCount() == 2) {
					EventQueue.invokeLater(new Runnable() {
						public void run() {
							WorkerDetails page = null;
							try {
								page = new WorkerDetails(parent,db,admin.getUserById(Integer.parseInt(String.valueOf(clientTable
												.getValueAt(clientTable
														.getSelectedRow(), 0)))),admin);
							} catch (NumberFormatException e) {
								
								e.printStackTrace();
							} catch (SQLException e) {
								
								e.printStackTrace();
							}
							page.setVisible(true);
							parent.setVisible(false);
						}
					});
				}
			}
				}
		});
		
		
	}
	
	private JScrollPane pane;
	private JLabel filterInfo;
	private JComboBox<String> filter;
	private Admin admin;
	private TableModel model;
	private JTable clientTable;
	private String[] workerCollumnNames;
	private String[] parcelCollumnNames;
	private String[][] data;
	private AdminHomePage parent;
	private Communication db;
	private int panelType;
	
	public static final int PARCEL_PANEL = 1;
	public static final int WORKER_PANEL = 2;
}
